
#include "Protocol.hpp"

class Calculator
{
public:
    Calculator()
    {
    }
    ~Calculator()
    {
    }

    Response Execute(const Request req)
    {
        Response res;
        switch (req.GetOp())
        {
        case '+':
            res.SetResult(req.GetX() + req.GetY());
            break;
        case '-':
            res.SetResult(req.GetX() - req.GetY());
            break;
        case '*':
            res.SetResult(req.GetX() * req.GetY());
            break;
        case '/':
            {
                if(req.GetY() == 0)
                    res.SetCode(1);     //除0错误
                else
                    res.SetResult(req.GetX() / req.GetY());
            }
            break;
        case '%':
            {
                if(req.GetY() == 0)
                    res.SetCode(2);     //模0错误
                else
                    res.SetResult(req.GetX() % req.GetY());
            }
            break;
        default:
            res.SetCode(3);     //运算符无法识别
            break;
        }
        return res;
    }
};